<?php

class ShopController extends BaseController
{

    /**
     * 商品列表
     * @return mixed
     */
    public function ProductList()
    {
        $info = ShopService::instance()->ProductList();
        return $this->view('shop.product_list')->with('info', $info);
    }

    /**
     * 商品录入
     * @return mixed
     */
    public function ProductAdd()
    {
        $business  = ShopService::instance()->BusinessList();
        $brand     = ShopService::instance()->BrandList();
        $sort      = ShopService::instance()->CatesList();
        $spec      = ShopService::instance()->Specificate();
        return $this->view('shop.product_add')->with(['business'=>$business,'brand'=>$brand,'sort'=>$sort,'spec'=>$spec,]);
    }

    /**
     * 商品编辑
     * @return mixed
     */
    public function ProductEdit()
    {
        if (Request::isMethod('POST')) {
            #收集信息
            $paramObj                   = new stdClass();
            $paramObj->id               = $this->getParam('id');                         //产品id
            $paramObj->product_name     = $this->getParam('product_name');               //产品名称
            $paramObj->price            = $this->getParam('price');                      //价格
            $paramObj->brand            = $this->getParam('brand');                      //品牌
            $paramObj->spec             = $this->getParam('spec');                       //规格
            $paramObj->sort             = $this->getParam('sort');                       //分类
            $paramObj->stock            = $this->getParam('stock');                      //库存
            $paramObj->place            = $this->getParam('place');                      //产地
            $paramObj->business_id      = $this->getParam('business_id');                //商家id
            $paramObj->img1             = $this->getParam('img1');                       //图片
            $paramObj->img2             = $this->getParam('img2');                       //图片
            $paramObj->img3             = $this->getParam('img3');                       //图片
            $paramObj->introduce        = $this->getParam('introduce');                  //产品介绍
            $paramObj->is_show          = $this->getParam('is_show');                    //显示状态
            $paramObj->img1_old         = $this->getParam('img1_old');                   //图片
            $paramObj->img2_old         = $this->getParam('img2_old');                   //图片
            $paramObj->img3_old         = $this->getParam('img3_old');                   //图片

            #逻辑操作
            empty($paramObj->id) ? ShopService::instance()->ProductAdd($paramObj) : ShopService::instance()->ProductUP($paramObj);
//                (ShopService::instance()->FCompanyAdd($paramObj).
//                    LogService::instance()->write(['content' => '保理公司添加'.$paramObj->f_company_name, 'type' => '保理公司信息'])):
//                (ShopService::instance()->FCompanyUp($paramObj).
//                    LogService::instance()->write(['content' => '保理公司修改'.$paramObj->f_company_name, 'type' => '保理公司信息']));
            return Redirect::to('/product_list');
        } else {
            $id = $this->getParam('id');
            $business  = ShopService::instance()->BusinessList();
            $brand     = ShopService::instance()->BrandList();
            $sort      = ShopService::instance()->CatesList();
            $spec      = ShopService::instance()->Specificate();
            $info = ShopService::instance()->GetProductId($id);
            return $this->view('shop.product_add')->with(['info'=>$info,'business'=>$business,'brand'=>$brand,'sort'=>$sort,'spec'=>$spec,]);
        }
    }

    /**
     * 商家列表
     * @return mixed
     */
    public function BusinessList()
    {
        $info = ShopService::instance()->BusinessList();
        return $this->view('shop.business_list')->with('info', $info);
    }

    /**
     * 商家录入
     * @return mixed
     */
    public function BusinessAdd()
    {
        return $this->view('shop.business_add');
    }

    /**
     * 商家编辑
     * @return mixed
     */
    public function BusinessEdit()
    {
        if (Request::isMethod('POST')) {
            #收集信息
            $paramObj                   = new stdClass();
            $paramObj->id               = $this->getParam('id');                            //商家id
            $paramObj->business_name    = $this->getParam('business_name');                 //商家名称
            $paramObj->person           = $this->getParam('person');                        //联系人
            $paramObj->phone            = $this->getParam('phone');                         //联系电话
            $paramObj->area             = $this->getParam('area');                          //地区
            $paramObj->logo             = $this->getParam('logo');                          //logo
            $paramObj->logo_old         = $this->getParam('logo_old');                      //logo
            $paramObj->introduce        = $this->getParam('introduce');                     //商家介绍
            $paramObj->is_show          = $this->getParam('is_show');                       //显示状态

            #逻辑操作
            empty($paramObj->id) ? ShopService::instance()->BusinessAdd($paramObj) : ShopService::instance()->BusinessUp($paramObj);
//                (ShopService::instance()->FCompanyAdd($paramObj).
//                    LogService::instance()->write(['content' => '保理公司添加'.$paramObj->f_company_name, 'type' => '保理公司信息'])):
//                (ShopService::instance()->FCompanyUp($paramObj).
//                    LogService::instance()->write(['content' => '保理公司修改'.$paramObj->f_company_name, 'type' => '保理公司信息']));
            return Redirect::to('/business_list');
        } else {
            $id = $this->getParam('id');
            $info = ShopService::instance()->GetBusinessId($id);
            return $this->view('shop.business_add')->with('info', $info);
        }

    }

    /**
     *商城分类列表页  AddCategory
     */
    public function CategoryList()
    {
        $info = ShopService::instance()->CatesList();
        foreach ($info as $k => $v) {
            $info[$k]->cat_name = ShopService::instance()->SelectCategory($v->c_id, $v->fid);
        }
        return $this->view('shop.product_category')->with(['category' => $info]);
    }

    /**
     *商城添加分类
     */
    public function AddCategory()
    {
        #接收参数
        $paramObj = new stdClass();
        $paramObj->c_id = $this->getParam('c_id');
        if (!empty($paramObj->c_id)) {
            $info = ShopService::instance()->CatListfa();
            $infirst = ShopService::instance()->CatcFirst($paramObj);
//            print_r($infirst);
            return $this->view('shop.product_category_import')->with(['cid' => $infirst, 'category' => $info]);
        } else {
            $info = ShopService::instance()->CatListfa();
            return $this->view('shop.product_category_import')->with(['category' => $info]);
        }
    }

    /**
     * 添加动作
     * @author xf
     * @date
     */
    public function AjaxAddCategory()
    {
        #接收参数
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->cate_name = $this->getParam('cate_name');
            $paramObj->fid = $this->getParam('fid');
            #逻辑操作
            ShopService::instance()->AddCategory($paramObj);
            return Redirect::to('/category_list');
        }
    }

    /**
     * 修改动作
     * @author xf
     * @date
     */
    public function AjaxupdateCategory()
    {
        #接收参数
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->cate_name = $this->getParam('cate_name');
            $paramObj->fid = $this->getParam('fid');
            $paramObj->c_id = $this->getParam('c_id');
            #逻辑操作
            ShopService::instance()->upCategory($paramObj);
            return Redirect::to('/category_list');
        }
    }

    /**
     * 删除分类
     * @author xf
     * @date
     */
    public function DelectCategory()
    {
        #接收参数
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->c_id = $this->getParam('bank_num');
            #逻辑操作
            $data = ShopService::instance()->FatherCategory($paramObj);//查询是父级还是子级
            if (empty($data->fid)) {//父级s
                $data = ShopService::instance()->ParpentCategory($paramObj);//查询父级下面的子级
                if (empty($data)) {
                    $da = ShopService::instance()->DelectCategory($paramObj);
                    if (empty($da)) {
                        $mess['code'] = '2';
                        $mess['mess'] = '参数错误！！!';
                        return $mess;
                    } else {
                        $mess['code'] = '1';
                        $mess['mess'] = '删除成功！！!';
                        return $mess;
                    }
                } else {
                    $mess['code'] = '2';
                    $mess['mess'] = '请先删除子类';
                    return $mess;
                }

            } else {
                $data = ShopService::instance()->DelectCategory($paramObj);
                if (empty($data)) {
                    $mess['code'] = '2';
                    $mess['mess'] = '参数错误！！!';
                    return $mess;
                } else {
                    $mess['code'] = '1';
                    $mess['mess'] = '删除成功！！!';
                    return $mess;
                }

//                return Redirect::to('/category_list');
            }
            //print_r($data->fid) ;

        }
    }

    /**
     * 规格列表
     * @author xf
     * @date
     */
    public function Specificate()
    {
        $info = ShopService::instance()->Specificate();
//        print_r($info);
//        die;
////        foreach($info as $k =>$v){
////            $info[$k]->cat_name=ShopService::instance()->SelectSpecificate($v->s_id,$v->fid);
////        }
        return $this->view('shop.product_specificate')->with(['category' => $info]);
    }

    /**
     *商城添加规格
     */
    public function AddSpecificate()
    {
        #接收参数
        $paramObj = new stdClass();
        $paramObj->s_id = $this->getParam('s_id');
        if (!empty($paramObj->s_id)) {
            $info = ShopService::instance()->CatListfa();
            $infirst = ShopService::instance()->SpecFirst($paramObj);
            return $this->view('shop.product_specificate_import')->with(['cid' => $infirst, 'category' => $info]);
        } else {
            $info = ShopService::instance()->CatListfa();
            return $this->view('shop.product_specificate_import')->with(['category' => $info]);
        }
    }

    /**
     * 添加规格动作
     * @author xf
     * @date
     */
    public function AjaxAddSpecificate()
    {
        #接收参数
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->spec_name = $this->getParam('spec_name');
            $paramObj->fid = $this->getParam('fid');
            #逻辑操作
            ShopService::instance()->AddSpecificate($paramObj);
            return Redirect::to('/specificate');
        }
    }

    /**
     * 修改动作
     * @author xf
     * @date
     */
    public function AjaxupSpecificate()
    {
        #接收参数
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->spec_name = $this->getParam('spec_name');
            $paramObj->fid = $this->getParam('fid');
            $paramObj->s_id = $this->getParam('s_id');
            #逻辑操作
            ShopService::instance()->upSpecificate($paramObj);
            return Redirect::to('/specificate');
        }
    }

    /**
     * 删除分类
     * @author xf
     * @date
     */
    public function DelectSpecificate()
    {
        #接收参数
        if (Request::isMethod('POST')) {
            $paramObj = new stdClass();
            $paramObj->s_id = $this->getParam('bank_num');
            #逻辑操作
            $data = ShopService::instance()->DelectSpecificate($paramObj);
            if (empty($data)) {
                $mess['code'] = '2';
                $mess['mess'] = '参数错误！！!';
                return $mess;
            } else {
                $mess['code'] = '1';
                $mess['mess'] = '删除成功！！!';
                return $mess;
            }

//                return Redirect::to('/category_list');
        }
        //print_r($data->fid) ;

    }

    /**
     * 品牌列表
     * @return mixed
     */
    public function BrandList()
    {
        $info = ShopService::instance()->BrandList();
        return $this->view('shop.product_brand')->with('info', $info);
    }

    /**
     * 品牌添加
     * @return mixed
     */
    public function BrandAdd()
    {
        return $this->view('shop.product_brand_import');
    }

    /**
     * 品牌编辑
     * @return mixed
     */
    public function BrandEdit()
    {
        if (Request::isMethod('POST')) {
            #收集信息
            $paramObj = new stdClass();
            $paramObj->id          =  $this->getParam('id');                         //品牌id
            $paramObj->brand_name  =  $this->getParam('brand_name');                 //品牌名称
            $paramObj->is_show     =  $this->getParam('is_show');                    //是否显示


            #逻辑操作
            empty($paramObj->id) ? ShopService::instance()->BrandAdd($paramObj) : ShopService::instance()->BrandUp($paramObj);
//                (ShopService::instance()->FCompanyAdd($paramObj).
//                    LogService::instance()->write(['content' => '保理公司添加'.$paramObj->f_company_name, 'type' => '保理公司信息'])):
//                (ShopService::instance()->FCompanyUp($paramObj).
//                    LogService::instance()->write(['content' => '保理公司修改'.$paramObj->f_company_name, 'type' => '保理公司信息']));
            return Redirect::to('/brand_list');
        } else {
            $id = $this->getParam('id');
            $info = ShopService::instance()->GetBrandId($id);
            return $this->view('shop.product_brand_import')->with('info', $info);
        }
    }

    /**
     * 品牌删除
     */
    public function BrandDel()
    {
        #收集信息
        $aData = new stdClass();
        $aData->id = $this->getParam('id');

        //$name = AdminService::instance()->GetAdminInfo($aData->id)->name;
        #逻辑操作
        $state = ShopService::instance()->BrandDel($aData);
        //LogService::instance()->write(['content' => '删除用户'.$name, 'type' => '个人中心']);
        $this->rest->success(['state' => $state], ErrorCodeEnum::STATUS_SUCCESS, '刪除成功！');

    }

    /**
     *品牌删除ajax返回
     */
    public function BrandDelAjax()
    {
        $info = ShopService::instance()->BrandList();
        return $this->viewAjax('admin.ajaxTemplates.shop_brand_list_part')->with('info', $info);
    }

}
